package algorithm;

public class leetcode_0042 {
    public int trap(int[] height) {
        int len=height.length;
        int current;
        int capacity=0;
        int max=0;
        for(int i=0;i<len;++i){
            max=(max>height[i])?max:height[i];
        }
        for(int i=0;i<max;++i){
            capacity+=floor(height,len);
        }
        return capacity;
    }
    public int floor(int[]height,int len){
        int capacity=0;int current=0;
        for(int i=1;i<len-1;++i){
            if(height[i-1]!=0&&height[i]==0){
                current++;
            }
            else if(height[i]==0){
                if(current!=0) current++;
            }
            if(height[i+1]!=0){
                capacity+=current;
                current=0;
            }
        }
        for(int i=0;i<len;++i){
            if(height[i]>0) height[i]-=1;
        }
        return capacity;
    }
}
